<template>
	<view>
		<totalMask :show.sync="isShow" :showType="showType">
			<view class="nvue-flex">
				<view class="title-bg">
					<text class="title">邀请连麦列表</text>
				</view>
				
				<scroll-view scroll-y class="list" :show-scrollbar="false" @scrolltolower="onLoadMore">
					<view class="view">
						<block v-for="(item, index) in list" :key="index">
							<view class="item">
								<image class="avatar" :src="item.user_avatar"></image>
				
								<view class="nickname-bg">
									<text class="nickname">{{item.user_nickname}}</text>
								</view>
				
								<view class="lm-btn" @click="onSure(item)">
									<text class="lm-title" v-if="item.type == 1">视频连麦</text>
									<text class="lm-title" v-if="item.type == 2">语音连麦</text>
								</view>
							</view>
						</block>
					</view>
				</scroll-view>
			</view>
		</totalMask>
	</view>
</template>

<script>
	import storage from "@/utils/storage";
	import st from "@/utils/showtoast.js";
	import {
		inviteLMList,
		zbAgreeLM,
		zbApplyLM
	} from "@/API/newLive.js";
	
	export default {
		data() {
			return {
				isShow: false,
				showType: 0,
				page : 1,
				total: 0,
				list: [],
			}
		},
		destroyed() {
			uni.$off("ZBInviteLM")
		},
		mounted() {
			uni.$on("ZBInviteLM", () => {
				this.isShow = true
				this.showType = 7
				
				this.onRefresh()
			});
		},
		methods: {
			/**
			 * 确认连麦
			 */
			async onSure(item) {
				// let res = await zbAgreeLM(storage.getLiveRoomInfo().id, item.id)
				// st.success("连麦成功")
				// storage.setLMUserID(item.user_id)
				// storage.setLMItemID(item.id)
				// uni.$emit("ZBAgreeLMAfter", item.type)
				let res = await zbApplyLM(storage.getLiveRoomInfo().id, item.type, item.user_id)
				st.show("等待用户同意");
				this.isShow = false
			},
			/**
			 * 加载更多
			 */
			onLoadMore() {
				if (this.list.length < this.total) {
					this.page++
					this.getData()
				}
			},
			
			/**
			 * 刷新
			 */
			onRefresh() {
				this.page = 1
				this.list = []
				this.getData()
			},
			
			/**
			 * 网络请求
			 */
			async getData() {
				let res = await inviteLMList(storage.getLiveRoomInfo().id, this.page)
				this.total = res.data.total
				let list = res.data.data
				if (this.page == 1) {
					this.list = list
				} else {
					this.list = [...this.list, ...list]
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.title-bg {
		width: 300rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}
	.title {
		color: #191919;
		font-size: 32rpx;
	}
	
	.list {
		margin-top: 30rpx;
		width: 750rpx;
		overflow: hidden;
		height: 600rpx;
	}
	
	.view {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}
	
	.item {
		width: 750rpx;
		padding: 0 35rpx;
		margin-bottom: 45rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		overflow: hidden;
	}
	
	.avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		overflow: hidden;
		background-color: red;
	}
	
	.nickname-bg {
		margin-left: 30rpx;
		margin-right: 30rpx;
		flex: 1;
		overflow: hidden;
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	
	.nickname {
		color: #1B1B1B;
		font-size: 30rpx;
	}
	
	.lm-btn {
		height: 52rpx;
		border-radius: 26rpx;
		overflow: hidden;
		background: linear-gradient(to right, #4FB784, #A4D79F);
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0 27rpx;
	}
	
	.lm-title {
		color: white;
		font-size: 24rpx;
	}
</style>